package com.kantboot.business.water.repository;

import com.kantboot.business.water.domain.dto.BusWaterGoodsOrderSearchDTO;
import com.kantboot.business.water.domain.dto.BusWaterGoodsOrderSearchOfAdminDTO;
import com.kantboot.business.water.domain.dto.BusWaterGoodsOrderSearchOfManageDTO;
import com.kantboot.business.water.domain.entity.BusWaterGoodsOrder;
import com.kantboot.functional.pay.order.domian.entity.FunctionalPayOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface BusWaterOrderRepository extends JpaRepository<BusWaterGoodsOrder,Long> {


    BusWaterGoodsOrder findByPayOrderId(Long payOrderId);

    List<BusWaterGoodsOrder> findByOrderStatusCode(String payStatu);

    List<BusWaterGoodsOrder> findByuserIdOfConsumer(Long selfId);

    Long countByUserIdOfConsumerAndOrderStatusCode(Long userIdOfConsumer,String orderStatusCode);

    @Query("""
        from BusWaterGoodsOrder AS g
        LEFT JOIN BusWaterLiftingPoint AS p
        ON g.waterLiftingPointId = p.id
        LEFT JOIN BusWaterGoods AS w
        ON g.goodsId = w.id
        where (:#{#param.orderStatusCode} is null or g.orderStatusCode = '' or g.orderStatusCode = :#{#param.orderStatusCode})
        and (:#{#param.orderStatusCode} is null or g.orderStatusCode = '' or g.orderStatusCode = :#{#param.orderStatusCode})
        and (:#{#param.nameOfConsumer} is null or g.nameOfConsumer = '' or g.nameOfConsumer like concat('%',:#{#param.nameOfConsumer},'%'))
        and (:#{#param.phoneOfConsumer} is null or g.phoneOfConsumer = '' or g.phoneOfConsumer like concat('%',:#{#param.phoneOfConsumer},'%'))
        and (:#{#param.liftingPointName} is null or p.name = '' or p.name like concat('%',:#{#param.liftingPointName},'%'))
        and (:#{#param.goodsName} is null or w.name = '' or w.name like concat('%',:#{#param.goodsName},'%'))
        and (:#{#param.goodsDescription} is null or w.description = '' or w.description like concat('%',:#{#param.goodsDescription},'%'))
        and (:#{#param.payOrderId} is null or g.payOrderId = :#{#param.payOrderId})
    """)
    Page<BusWaterGoodsOrder> getBodyData(@Param("param") BusWaterGoodsOrderSearchOfAdminDTO goods, Pageable pageable);

    /**
     * 提水点id在liftingPointIds中的订单
     * @param goods
     * @param pageable
     * @return
     */
    @Query("""
        from BusWaterGoodsOrder
        where (:#{#param.orderStatusCode} is null or :#{#param.orderStatusCode} = '' or orderStatusCode = :#{#param.orderStatusCode})
        and (:#{#param.userIdOfConsumer} is null or userIdOfConsumer = :#{#param.userIdOfConsumer})
        and waterLiftingPointId in (:#{#param.liftingPointIds})
    """)
    Page<BusWaterGoodsOrder> getBodyDataOfManage(@Param("param") BusWaterGoodsOrderSearchOfManageDTO goods, Pageable pageable);


    @Query("""
            FROM BusWaterGoodsOrder
            WHERE (
                :#{#param.orderStatusCode} IS NULL OR :#{#param.orderStatusCode} = '' OR  orderStatusCode = :#{#param.orderStatusCode}
            )
            AND (:#{#param.userIdOfConsumer} IS NULL OR userIdOfConsumer = :#{#param.userIdOfConsumer})
            AND payOrderStatusCode =""" +" '" + FunctionalPayOrder.STATUS_CODE_PAID + "'"
            + """
            ORDER BY gmtModified DESC
            """
    )
    Page<BusWaterGoodsOrder> getBodyDataFront(@Param("param")BusWaterGoodsOrderSearchDTO param,Pageable pageable);

}
